package com.sd.repay.service.impl;

import com.sd.repay.common.Constants;
import com.sd.repay.dao.BaseDao;
import com.sd.repay.pojo.ExtractionOrder;
import com.sd.repay.pojo.ReverseRecord;
import com.sd.repay.pojo.WithdrawOrder;
import com.sd.repay.service.ExtractOrderService;
import com.sd.repay.service.ReverseRecordService;
import com.sd.repay.service.WithdrawNotifyBaseService;
import com.sd.repay.utils.JsonUtil;
import com.sd.repay.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author sd
 * @Description: 统一出账结果提现类型订单通知Service实现
 * @date 2017年10月25日
 */
@Service
public class ExtractWithdrawServiceImpl implements WithdrawNotifyBaseService {

    private static Logger log = LoggerFactory.getLogger(ExtractWithdrawServiceImpl.class);

    @Autowired
    private BaseDao baseDao;
    @Autowired
    private ExtractOrderService extractOrderService;
    @Autowired
    private ReverseRecordService reverseService;

    /**
     * 提现类型统一出账订单业务通知
     *
     * @param withdrawOrder
     * @return
     * @throws Exception
     */
    @Override
    public String reciveWithdrawNotify(WithdrawOrder withdrawOrder) throws Exception {

        String extOrderNo = withdrawOrder.getServiceOrderNo();
        String notifyStatus = withdrawOrder.getStatus();
        String resultMsg = withdrawOrder.getResultMsg();
        String resultCode = withdrawOrder.getResultCode();

        log.info("提现类型统一出账订单业务通知处理参数extOrderNo：[{}]，notifyStatus：[{}]，resultMsg：[{}]，resultCode：[{}]", new Object[]{extOrderNo, notifyStatus, resultMsg, resultCode});
        if (StringUtil.isBlank(extOrderNo, notifyStatus)) {
            return "FAIL";
        }
        //获取提现订单信息
        ExtractionOrder extOrder = extractOrderService.getExtractByOrderNo(extOrderNo);
        log.info("提现类型统一出账订单业务通知extOrderNo：[{}]，对应的提现订单信息：[{]]", new Object[]{extOrderNo, JsonUtil.objectToJson(extOrder)});
        if (extOrder == null) {
            return "FAIL";
        }
        String oldExtStatus = extOrder.getStatus();
        int count = 0;
        //提现成功
        if (Constants.WITHDRAW_STATUS_SUCCESS.equals(notifyStatus)) {
            //修改提现记录状态为提现成功，且不需要冲正
            count = extractOrderService.updateExtractStatus(extOrderNo, Constants.EXTRACT_STATUS_SUCCESS, oldExtStatus, Constants.EXTRACT_IS_NOT_REVERSE, "", resultCode, resultMsg);
            log.info("提现类型统一出账订单业务通知extOrderNo：[{}]提现成功，修改状态影响的行数：[{]]", new Object[]{extOrderNo, count});
            if (count < 0) {
                return "FAIL";
            }
        } else if (Constants.WITHDRAW_STATUS_FAIL.equals(notifyStatus)) {
            //提现失败

            //查询是否已有冲正成功的记录
            ReverseRecord record = reverseService.getReverseRecordByService(Constants.WITHDRAW_SERVICE_WITHDRAW, extOrderNo);
            if (record == null || !Constants.REVERSE_STATUS_SUCCESS.equals(record.getStatus())) {

                count = extractOrderService.updateExtractStatus(extOrderNo, Constants.EXTRACT_STATUS_FAIL, oldExtStatus, Constants.EXTRACT_IS_REVERSE, "", resultCode, resultMsg);
                log.info("提现类型统一出账订单业务通知extOrderNo：[{}]提现失败，且需要冲正，修改状态影响的行数：[{]]", new Object[]{extOrderNo, count});
                if (count < 0) {
                    return "FAIL";
                }
            } else {

                count = extractOrderService.updateExtractStatus(extOrderNo, Constants.EXTRACT_STATUS_FAIL, oldExtStatus, Constants.EXTRACT_IS_NOT_REVERSE, Constants.EXTRACT_REVERSE_SUCCESS, resultCode, resultMsg);
                log.info("提现类型统一出账订单业务通知extOrderNo：[{}]提现失败，且已冲正成功，修改状态影响的行数：[{]]", new Object[]{extOrderNo, count});
                if (count < 0) {
                    return "FAIL";
                }
            }
        }
        return "SUCCESS";
    }
}